package date.calendar;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class CalendarTest {

    public static void main(String[] args) {
        Calendar c = Calendar.getInstance();
        c.setTime(new Date());// 设置一年的第一天
        System.out.println(c.get(Calendar.DAY_OF_WEEK));//

        System.out.println("当前时间:" + c.getTime());

        System.out.println("本月第" + c.get(Calendar.WEEK_OF_MONTH) + "周");

        System.out.println("今年的第" + c.get(Calendar.WEEK_OF_YEAR) + "周");

        System.out.println(c.get(Calendar.DAY_OF_YEAR));

        System.out.println(c.get(Calendar.DST_OFFSET));

        int weeks = new CalendarTest().getDay(c);

        System.out.println("第" + weeks + "周");

        System.out.println(c.getTimeZone());// 地域

        System.out.println("==================================");
        try {
            String dateStr1 = "2020-12-30";
            String dateStr2 = "2021-01-03";

            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

            Calendar c1 = Calendar.getInstance();
            c1.setTime(sdf.parse(dateStr1));

            Calendar c2 = Calendar.getInstance();
            c2.setTime(sdf.parse(dateStr2));

            System.out.println(c1.getActualMaximum(Calendar.DAY_OF_YEAR));
            System.out.println("日期天数差：" + (c2.get(Calendar.DAY_OF_YEAR) + (c1.getActualMaximum(Calendar.DAY_OF_YEAR) - c1.get(Calendar.DAY_OF_YEAR))));

            System.out.println("年相差:" + (c2.get(Calendar.YEAR) - c1.get(Calendar.YEAR)));

        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

    public int getDay(Calendar c) {
        int i = c.get(Calendar.DAY_OF_WEEK);
        Calendar cn = Calendar.getInstance();
        cn.setTime(new Date());
        int weeks = 0;
        if (i != 1) {
            weeks = (cn.get(Calendar.DAY_OF_YEAR) - (8 - i)) % 7;
            if (weeks == 0)
                weeks = (cn.get(Calendar.DAY_OF_YEAR) - (8 - i)) / 7;
            else
                weeks = (cn.get(Calendar.DAY_OF_YEAR) - (8 - i)) / 7 + 1;
            weeks++;
        } else {
            weeks = (cn.get(Calendar.DAY_OF_YEAR)) % 7;
            if (weeks == 0)
                weeks = (cn.get(Calendar.DAY_OF_YEAR)) / 7;
            else
                weeks = (cn.get(Calendar.DAY_OF_YEAR)) / 7 + 1;
        }

        return weeks;
    }
}
